package com.rchat.pocmini.util;

import android.app.Service;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.Log;

import com.rchat.pocmini.R;

/************************************************
 * Sound handler
 ************************************************/

public class Sound {
    private static final String TAG = "Sound";

    public static final int PLAYER_IDS_MAX = 18;

    private static final int[] PLAYER_SOUNDS = {0, // PLAYER_NEWINFO
            R.raw.sound_media_me_on, // PLAYER_MEDIA_ME_ON
            R.raw.sound_media_me_off, // PLAYER_MEDIA_ME_OFF
            R.raw.sound_media_other_on, // PLAYER_MEDIA_OTHER_ON
            R.raw.sound_media_other_off, // PLAYER_MEDIA_OTHER_OFF
            R.raw.sound_media_knock, // PLAYER_MEDIA_KNOCK
            R.raw.sound_media_error, // PLAYER_MEDIA_ERROR
            R.raw.sound_callbegin, // PALYER_CALL_BEGIN
            R.raw.sound_callend, // PALYER_CALL_END
            R.raw.sound_callerror, // PALYER_CALL_ERROR
            R.raw.sound_call_dial // PLAYER_CALL_DIAL
    };

//	private static final String[] PLAYER_SOUNDS = { "", // PLAYER_NEWINFO
//			getPath("sound_media_me_on"), // PLAYER_MEDIA_ME_ON
//			getPath("sound_media_me_off"), // PLAYER_MEDIA_ME_OFF
//			getPath("sound_media_other_on"), // PLAYER_MEDIA_OTHER_ON
//			getPath("sound_media_other_off"), // PLAYER_MEDIA_OTHER_OFF
//			getPath("sound_media_knock"), // PLAYER_MEDIA_KNOCK
//			getPath("sound_media_error"), // PLAYER_MEDIA_ERROR
//			getPath("sound_callbegin"), // PALYER_CALL_BEGIN
//			getPath("sound_callend"), // PALYER_CALL_END
//			getPath("sound_callerror"), // PALYER_CALL_ERROR
//			getPath("sound_call_dial") // PLAYER_CALL_DIAL
//	};
//
//	public static String getPath(String name){
//		StringBuilder sb = new StringBuilder();
//		sb.append(Environment.getExternalStorageDirectory());
//		sb.append(File.separator);
//		sb.append(PocApplication.getInstance().getPackageName());
//		sb.append(File.separator);
//		sb.append("raw");
//		sb.append(File.separator);
//		sb.append(name).append(".ogg");
//		return sb.toString();
//	}

    public static final int PLAYER_NEWINFO = 0;
    public static final int PLAYER_MEDIA_ME_ON = 1;
    public static final int PLAYER_MEDIA_ME_OFF = 2;
    public static final int PLAYER_MEDIA_OTHER_ON = 3;
    public static final int PLAYER_MEDIA_OTHER_OFF = 4;
    public static final int PLAYER_MEDIA_KNOCK = 5;
    public static final int PLAYER_MEDIA_ERROR = 6;
    public static final int PLAYER_CALL_BEGIN = 7;
    public static final int PLAYER_CALL_END = 8;
    public static final int PLAYER_CALL_ERROR = 9;
    public static final int PLAYER_CALL_DIAL = 10;
    public static final int PLAYER_INCOMING_RING = 11;

    public static MediaPlayer[] mediaPlayer = null;
    private static boolean alert = true;
    private static Vibrator vibrator = null;
    private static String currentSystemMusic = "";

    public static void setSoundAlert(boolean silent) {
        alert = silent;
    }

    public static boolean isAlert() {
        return alert;
    }

    public static void vibrate(Context context) {
        if (context != null) {
            vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
            if (vibrator != null)
                vibrator.vibrate(50);
        }
    }

    public static void vibrate(int msecond, Context context) {
        if (context != null) {
            vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
            if (vibrator != null)
                vibrator.vibrate(msecond);
        }
    }

    public static void playSound(int playerId, Context context) {
        playSound(playerId, false, context, null);
    }

    public static void playSound(int playerId, boolean isLooping, Context context, MediaPlayer.OnCompletionListener listener) {
        try {
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer[PLAYER_IDS_MAX];
            }
            switch (playerId) {
                // 引用系统提示音
                case PLAYER_NEWINFO:
                case PLAYER_INCOMING_RING: {
                    if (context != null) {
                        String name = (playerId == PLAYER_INCOMING_RING) ? Settings.System.RINGTONE
                                : Settings.System.NOTIFICATION_SOUND;
                        String systemMuc = Settings.System.getString(context.getContentResolver(), name);
                        if (listener != null) {
                            mediaPlayer[playerId].setOnCompletionListener(listener);
                        }
                        if (mediaPlayer[playerId] == null || !systemMuc.equals(currentSystemMusic)) {
                            currentSystemMusic = systemMuc;
                            mediaPlayer[playerId] = new MediaPlayer();
                            mediaPlayer[playerId].setDataSource(systemMuc);
                            mediaPlayer[playerId].prepare();
                        }
                        if (mediaPlayer[playerId] != null) {
                            mediaPlayer[playerId].start();
                            mediaPlayer[playerId].setLooping(isLooping);
                        }
                    }
                    break;
                }
                default: {
                    if (mediaPlayer[playerId] == null) {

                        mediaPlayer[playerId] = MediaPlayer.create(context, PLAYER_SOUNDS[playerId]);
                    }
                    if (listener != null) {
                        mediaPlayer[playerId].setOnCompletionListener(listener);
                    }
//				if (mediaPlayer[playerId] == null) {
//					mediaPlayer[playerId] = new MediaPlayer();
//					mediaPlayer[playerId].setDataSource(PLAYER_SOUNDS[playerId]);
//					mediaPlayer[playerId].prepare();
//				}
                    if (mediaPlayer[playerId] != null) {
                        if (!mediaPlayer[playerId].isPlaying()) {
                            mediaPlayer[playerId].start();
                            mediaPlayer[playerId].setLooping(isLooping);
                        }
                    } else {
                    }
                    break;
                }
            }

        } catch (Exception e) {
        }
        // soundPlaying = false;

    }

    public static void clearSound() {
        try {
            if (mediaPlayer == null)
                return;
            for (MediaPlayer mp : mediaPlayer) {
                if (mp != null) {
                    mp.stop();
                    mp.release();
                }
            }
            mediaPlayer = null;
        } catch (Exception e) {
        }
    }

    public static void stopSound(int playerId) {
        try {
            if (mediaPlayer != null && mediaPlayer[playerId] != null) {
                mediaPlayer[playerId].stop();
                mediaPlayer[playerId] = null;
            }
        } catch (Exception e) {
        }
    }

}
